package com.zhenmaitang.clinic_sys.util;

import com.zhenmaitang.clinic_sys.common.AuthResultCode;
import com.zhenmaitang.clinic_sys.common.CodeEnum;
import com.zhenmaitang.clinic_sys.common.ResultCode;
import com.zhenmaitang.clinic_sys.exception.BusinessException;
public class ApiResponse<T> {
    private boolean success;
    private String code;
    private String message;
    private T data;
    private long timestamp;


    public ApiResponse() {
        this.timestamp = System.currentTimeMillis();
    }

    public ApiResponse(boolean success, String code, String message) {
        this();
        this.success = success;
        this.code = code;
        this.message = message;
    }

    public ApiResponse(boolean success, String code, String message, T data) {
        this(success, code, message);
        this.data = data;
    }

    public static <T> ApiResponse<T> success(String message) {
        return new ApiResponse<>(true, String.valueOf(ResultCode.SUCCESS.getCode()), message);
    }

    public static <T> ApiResponse<T> success(T data) {
        return new ApiResponse<>(true, String.valueOf(ResultCode.SUCCESS.getCode()), "操作成功", data);
    }

    public static <T> ApiResponse<T> success(String message, T data) {
        return new ApiResponse<>(true, String.valueOf(ResultCode.SUCCESS.getCode()), message, data);
    }
    public static <T> ApiResponse<T> success(CodeEnum res, T data) {
        return new ApiResponse<>(true, String.valueOf(res.getCode()), res.getMessage(), data);
    }

    public static <T> ApiResponse<T> error(String message) {
        return new ApiResponse<>(false, String.valueOf(ResultCode.INTERNAL_SERVER_ERROR.getCode()), message);
    }

    public static <T> ApiResponse<T> error(int code, String message) {
        return new ApiResponse<>(false, String.valueOf(code), message);
    }

    public static <T> ApiResponse<T> error(ResultCode resultCode) {
        return new ApiResponse<>(false, String.valueOf(resultCode.getCode()), resultCode.getMessage());
    }

    public static <T> ApiResponse<T> error(AuthResultCode resultCode) {
        return new ApiResponse<>(false, String.valueOf(resultCode.getCode()), resultCode.getMessage());
    }

    public static <T> ApiResponse<T> error(BusinessException e) {
        return new ApiResponse<>(false, String.valueOf(e.getCode()), e.getMessage());
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getCode() {
        return code;
    }
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
}